using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor
{
    public partial class bmg : Form
    {
        List<List<byte>> hex = new List<List<byte>>();
    
        public byte[] file_;
        public bmg(string file)
        {
            InitializeComponent();
            file_ = File.ReadAllBytes(file);
        }
        public bmg(byte[] file)
        {
            InitializeComponent();
            file_ = file;
        }
        byte[] filler = new byte[12];
        byte[] filler2 = new byte[4];
        int nrMes = 0;
        List<int> offset = new List<int>();
        private void bmg_Load(object sender, EventArgs e)
        {
            EndianBinaryReader er = new EndianBinaryReader(new MemoryStream(file_), Endianness.LittleEndian);
            string txt = er.ReadString(Encoding.ASCII, 8);
            if (txt == "MESGbmg1" || txt == "GSEM1gmb")
            {
                er.ReadInt32();
                if (er.ReadInt32() == 2)
                {
                    er.ReadBytes(4);
                    filler = er.ReadBytes(12);
                    txt = er.ReadString(Encoding.ASCII, 4);
                    if (txt == "INF1" || txt == "1FNI")
                    {
                        er.ReadInt32();
                        nrMes = er.ReadInt16();
                        er.ReadInt16();
                        filler2 = er.ReadBytes(4);
                        for (int i = 0; i < nrMes; i++)
                        {
                            offset.Add(er.ReadInt32());
                        }
                        int try_ = 0;
                    try_:
                        txt = er.ReadString(Encoding.ASCII, 4);
                        if (txt == "DAT1" || txt == "1TAD")
                        {
                            er.ReadInt32();
                            long startindex = er.BaseStream.Position;
                            byte[] mes = er.ReadBytes((int)(er.BaseStream.Length - er.BaseStream.Position));
                            for (int i = 0; i < nrMes; i++)
                            {
                                if (i != nrMes - 1)
                                {
                                    byte[] new_ = new byte[offset[i + 1] - offset[i] - 2];
                                    hex.Add(new List<byte>());
                                    Array.Copy(mes, offset[i], new_, 0, offset[i + 1] - offset[i] - 2);
                                    hex[i].AddRange(new_);
                                    string str = "";
                                    for (int j = 0; j < new_.Length; j += 2)
                                    {
                                        char c = BitConverter.ToChar(new_, j);
                                        str += c;
                                    }
                                    listView1.Items.Add(str);
                                }
                                else
                                {
                                    byte[] new_ = new byte[(er.BaseStream.Length - startindex) - offset[i] - 2];
                                    hex.Add(new List<byte>());
                                    Array.Copy(mes, offset[i], new_, 0, (er.BaseStream.Length - startindex) - offset[i] - 2);
                                    hex[i].AddRange(new_);
                                    string str = "";
                                    for (int j = 0; j < new_.Length; j += 2)
                                    {
                                        char c = BitConverter.ToChar(new_, j);
                                        str += c;
                                    }
                                    listView1.Items.Add(str);
                                }
                                
                            }
                        }
                        else
                        {
                            goto try_;
                        }
                    }
                    else
                    {
                        MessageBox.Show("This section isn't INF1.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("The file don't have two sections.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("It isn't a bmg file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
       
        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                hexBox1.ByteProvider = new Be.Windows.Forms.DynamicByteProvider(hex[e.ItemIndex].ToArray());
                hexBox1.ByteProvider.Changed += new EventHandler(ByteProvider_Changed);
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }

        void ByteProvider_Changed(object sender, EventArgs e)
        {
            
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void hexBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Be.Windows.Forms.DynamicByteProvider t = new Be.Windows.Forms.DynamicByteProvider(new byte[1]);
            t = (Be.Windows.Forms.DynamicByteProvider)hexBox1.ByteProvider;
            string str = "";
            hex[listView1.SelectedIndices[0]].Clear();
            hex[listView1.SelectedIndices[0]].AddRange(t.Bytes.ToArray());
            for (int j = 0; j < hex[listView1.SelectedIndices[0]].Count; j += 2)
            {
                char c = BitConverter.ToChar(hex[listView1.SelectedIndices[0]].ToArray(), j);
                str += c;
            }
            listView1.Items[listView1.SelectedIndices[0]].Text = str;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            MemoryStream m = new MemoryStream();
            EndianBinaryWriter ew = new EndianBinaryWriter(m, Endianness.LittleEndian);
            ew.Write("MESGbmg1", Encoding.ASCII, false);
            int length = 0x3A;
            for (int i = 0; i < hex.Count; i++)
            {
                length += hex[i].Count + 6;
            }
            ew.Write(length);
            ew.Write(2);
            ew.Write(2);
            ew.Write(filler, 0, 12);
            ew.Write("INF1", Encoding.ASCII, false);
            ew.Write(0x10 + (hex.Count * 4));
            ew.Write((Int16)hex.Count);
            ew.Write((Int16)4);
            ew.Write(filler2, 0, 4);
            int offset2 = 2;
            ew.Write(offset2);
            for (int i = 0; i < hex.Count-1; i++)
            {
                offset2+=hex[i].Count+2;
                ew.Write(offset2);
            }
            ew.Write("DAT1", Encoding.ASCII, false);
            int length2 = 0xA;
            for (int i = 0; i < hex.Count; i++)
            {
                length2 += hex[i].Count + 2;
            }
            ew.Write(length2);
            ew.Write((Int16)0);
            for (int i = 0; i < hex.Count; i++)
            {
                ew.Write(hex[i].ToArray(), 0, hex[i].Count);
                ew.Write((Int16)0);
            }
            ew.Close();
            file_ = m.ToArray();
        }
    }
}
